(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
		});
	});
	function pagebtns(){
		check("anews-contents",isNull,"null");
		check("anotices-contents",isNull,"null");
		check("anotices-teachers",isNull,"null");
		//checkContents("anews-contents");
		//checkContents("anotices-contents");
		$("#anews-add").click(function(){
			var content = $("#anews-contents").val();
			var receivecode = $("input[name='anews-type']:checked").val();
			if(checkNews()){
				sendMessage(1,receivecode,content);
			}
		});
		$("#anotices-add").click(function(){
			var content = $("#anotices-contents").val();
			var receivecode = $("input[name='anotices-type']:checked").val();
			if(checkNotices()){
				sendMessage(2,receivecode,content);
			}
		});
		$("#anotices-send").click(function(){
			//判断，发送对象和内容不可谓空
			if($("#anotices-teachers").val() == ""){
				$("#anotices-teacher-ex").text("不能为空");
				return false;				
			}
			if($("#anotices-contents").val() == ""){
				$("#anotices-contents-ex").val("不能为空");
				return false;				
			}
			sendNotification(6);
		});
		$("#choice-teachers").unbind("click");
		$("#choice-teachers").click(function(){
			studentChoice(function(){
				$("#student-choice-name",parent.parent.document).hide();
				$("#student-choice-submit",parent.parent.document).hide();
				$(".selectL-title",parent.parent.document).text("待选教师");
				$(".selectR-title ",parent.parent.document).text("已选教师");
				$(".student-choice-title-name",parent.parent.document).text("选择教师");
				initDate();
				selectLR();
				$("#sub",parent.parent.document).unbind("click");
				$("#sub",parent.parent.document).click(function(){
					var teachername = [];
					var teachercode = [];
					var cellphone = [];
					var rightSel = $("#selectR",parent.parent.document);
					rightSel.find("option",parent.parent.document).each(function(){
						teachername.push(this.value);
						teachercode.push($(this).attr("teachercode"));
						cellphone.push($(this).attr("cellphone"));
					});
					var teachernames = teachername.join(",");
					var teachercodes = teachercode.join(",");
					var cellphones = cellphone.join(",");
					$(".index-dialog-cover",parent.parent.document).hide();
					$("#student-choice-dialog",parent.parent.document).hide();
					$("#anotices-teachers").val("");
					$("#anotices-teachers").val(teachernames);
					$("#anotices-teachers").attr("teachercodes",teachercodes);
					$("#anotices-teachers").attr("cellphones",cellphones);
					$("#selectR",parent.parent.document).empty();
					$("#anotices-teachers").focus();
				});
				$("#student-choice-submit",parent.parent.document).unbind("click");
				$("#student-choice-submit",parent.parent.document).click(function(){
					initDate();
				});
				document.onkeydown = function(e){ 
				    var ev = document.all ? window.event : e;
				    if(ev.keyCode==13) {
				    	initDate();
				    }
				};
			});
		});
	}
	function sendNotification(type){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var sendcode = $("#user-name",parent.parent.document).attr("code");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var receivecode = $("#anotices-teachers").attr("cellphones");
		var content = $("#anotices-contents").val();
		var cmpptype = $("input[name='anotices-cmpptype']:checked").val();
		var idatetime = Math.round(strToDate(getSmpFormatDate(new Date(),true))/1000);
		var data = {
				cmd: "sendMessage",
				model: "teacher",
				userKey:userKey,
				data: {
					type:type,
					sendcode: sendcode,
					receivecode: receivecode,
					content: encodeURI(content),
					cmpptype: cmpptype,
					idatetime:idatetime,
					schoolcode:schoolcode
				},
				remark: "",
		};
		processBegin("正在发送中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("发送成功");
			}else{
				processError("发送失败");
			}
		},null,null);
	};
	
	function sendMessage(type,receivecode,content){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var sendcode = $("#user-name",parent.parent.document).attr("code");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var idatetime = Math.round(strToDate(getSmpFormatDate(new Date(),true))/1000);
		var data = {
				cmd:"sendMessage",
				model:"teacher",
				userKey:userKey,
				data:{
					type:type,
					sendcode:sendcode,
					receivecode:receivecode,
					content:encodeURI(content),
					idatetime:idatetime,
					schoolcode:schoolcode
				},
				remark:"",
		};
		processBegin("正在发布中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("发布成功");
				
			}else{
				processError("发布失败");
			}
		},null,null);
	}
	function checkNews(){
		if($("#anews-contents").val() == ""){
			$("#anews-contents-ex").html("不能为空");
			return false;        			
		}else{
			return true;
		}
	}
	function checkNotices(){
		if($("#anotices-contents").val() == ""){
			$("#anotices-contents-ex").html("不能为空");
			return false;        			
		}else{
			return true;
		}
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
	
})();
